{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QuantLib as ql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.Enviroment\n",
    "settings = ql.Settings.instance()\n",
    "evDate = ql.Date(24, 3, 2023)\n",
    "settings.setEvaluationDate(evDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "September 15th, 2023\n"
     ]
    }
   ],
   "source": [
    "Cal = ql.China()\n",
    "DC360 = ql.Actual360()\n",
    "DC365 = ql.Actual365Fixed()\n",
    "settlementDays = 0\n",
    "refDate = Cal.advance(evDate, settlementDays, ql.Days, ql.Following, False)\n",
    "maturity = ql.Date.nthWeekday(3, ql.Friday, ql.September, 2023)\n",
    "print(maturity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.Market Data\n",
    "S0 = 4027.05\n",
    "QS = ql.SimpleQuote(S0)\n",
    "h_QS = ql.QuoteHandle(QS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4027.05\n"
     ]
    }
   ],
   "source": [
    "value = QS.value()\n",
    "print(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4027.05\n"
     ]
    }
   ],
   "source": [
    "value1 = h_QS.value()\n",
    "print(value1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = 0.0205407\n",
    "rTS = ql.FlatForward(settlementDays, Cal, r, DC360, ql.Simple, ql.Annual)\n",
    "h_rTS = ql.YieldTermStructureHandle(rTS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.020540699999999745\n"
     ]
    }
   ],
   "source": [
    "spotRate = h_rTS.zeroRate(0.25, ql.Simple, ql.Annual).rate()\n",
    "print(spotRate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.994891060299427\n"
     ]
    }
   ],
   "source": [
    "discFunc = h_rTS.discount(0.25)\n",
    "print(discFunc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "q = 0.0218\n",
    "qTS = ql.FlatForward(settlementDays, Cal, q, DC365, ql.Simple, ql.Annual)\n",
    "h_qTS = ql.YieldTermStructureHandle(qTS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "vol = 0.2\n",
    "volTS = ql.BlackConstantVol(evDate, Cal, vol, DC365)\n",
    "h_volTS = ql.BlackVolTermStructureHandle(volTS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2\n"
     ]
    }
   ],
   "source": [
    "strike = 4000\n",
    "tenor = 0.25\n",
    "marketVol = h_volTS.blackVol(tenor, strike)\n",
    "print(marketVol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "GBSProcess = ql.GeneralizedBlackScholesProcess(h_QS, h_qTS, h_rTS, h_volTS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
